package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.service.MemberService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;

    @Autowired
    private JedisPool jedisPool;

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        try {
            //获取原始文件名称
            String fileName = imgFile.getOriginalFilename();
            //截取文件扩展名
            String extension = fileName.substring(fileName.lastIndexOf(".") - 1);
            //构建新文件名称
            fileName = UUID.randomUUID().toString().replace("-", "") + extension;

            //执行文件上传
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);

            //保存图片名称到redis数据库
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);

            return new Result(true, MessageConstant.UPLOAD_SUCCESS, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Address address) {
        try {
            addressService.add(address);
            return new Result(true, MessageConstant.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.FAIL);
        }
    }

    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = addressService.pageQuery(queryPageBean);
        return pageResult;

    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        try {
            Address address = addressService.findById(id);
            return new Result(true, MessageConstant.SUCCESS, address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.FAIL);
        }
    }


    @PutMapping("/edit")
    public Result edit(@RequestBody Address address) {
        try {
            addressService.edit(address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.FAIL);
        }
        return new Result(true, MessageConstant.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        try {
            addressService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.FAIL);
        }
        return new Result(true, MessageConstant.SUCCESS);
    }


    @GetMapping("/findAllByMap")
    public Result findAllByMap() {
        try {
            List<Address> addressList = addressService.findAllByMap();
            return new Result(true, MessageConstant.SUCCESS, addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.FAIL);
        }
    }


}
